<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
  <title>商品列表</title>
  <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
</head>
<body>
<div class="container">
  <h1>商品列表</h1>
  <p>欢迎, ${sessionScope.user.username} | <a href="${pageContext.request.contextPath}/logout">退出登录</a></p>

  <c:if test="${not empty message}">
    <div class="alert success">${message}</div>
  </c:if>
  <c:if test="${not empty error}">
    <div class="alert error">${error}</div>
  </c:if>

  <table>
    <tr>
      <th>ID</th>
      <th>名称</th>
      <th>价格</th>
      <th>库存</th>
      <th>操作</th>
    </tr>
    <c:forEach items="${applicationScope.products}" var="product">
      <tr>
        <td>${product.id}</td>
        <td><a href="${pageContext.request.contextPath}/productDetail?id=${product.id}">${product.name}</a></td>
        <td>¥${product.price}</td>
        <td>${product.stock}</td>
        <td>
          <form action="${pageContext.request.contextPath}/cart" method="post">
            <input type="hidden" name="action" value="add">
            <input type="hidden" name="productId" value="${product.id}">
            <input type="number" name="quantity" value="1" min="1" max="${product.stock}">
            <button type="submit">加入购物车</button>
          </form>
        </td>
      </tr>
    </c:forEach>
  </table>

  <div class="cart-link">
    <a href="${pageContext.request.contextPath}/cart">查看购物车 (${not empty sessionScope.cart ? sessionScope.cart.size() : 0})</a>
  </div>
</div>
</body>
</html>